{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据已成功保存到 C:\\Users\\code\\Desktop\\2301班静态路由成绩.xlsx\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from bs4 import BeautifulSoup\n",
    "import os\n",
    "\n",
    "# HTML文件路径\n",
    "html_file_path = r\"C:\\Users\\code\\Downloads\\2301班静态路由成绩2.html\"\n",
    "\n",
    "# 读取HTML文件\n",
    "with open(html_file_path, 'r', encoding='utf-8') as file:\n",
    "    html_content = file.read()\n",
    "\n",
    "# 使用BeautifulSoup解析HTML\n",
    "soup = BeautifulSoup(html_content, 'html.parser')\n",
    "\n",
    "# 找到表格元素\n",
    "table = soup.find('table')\n",
    "\n",
    "# 提取表格数据\n",
    "rows = []\n",
    "for row in table.find_all('tr'):\n",
    "    cols = row.find_all('td')\n",
    "    cols = [col.text.strip() for col in cols]\n",
    "    rows.append(cols)\n",
    "\n",
    "# 将数据转换为DataFrame\n",
    "df = pd.DataFrame(rows)\n",
    "\n",
    "# 桌面路径\n",
    "desktop_path = os.path.join(os.path.expanduser(\"~\"), \"Desktop\")\n",
    "\n",
    "# 新的Excel文件路径\n",
    "excel_file_path = os.path.join(desktop_path, \"2301班静态路由成绩.xlsx\")\n",
    "\n",
    "# 保存DataFrame到Excel文件\n",
    "df.to_excel(excel_file_path, index=False, header=False)\n",
    "\n",
    "print(f\"数据已成功保存到 {excel_file_path}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
